import request from '@/utils/request'
import CustomForm from './Form/index.vue'
import RefundForm from './RefundForm/index.vue'
import Records from './Records/index.vue'
import FlowBox from '@/views/workFlow/components/FlowBox'

export default {
  components: { CustomForm, RefundForm, Records, FlowBox },
  data() {
    return {
      showAll: false,
      query: { customer: undefined },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      },
      formVisible: false,
      refundFormVisible: false,
      recordsVisible: false,
      exportBoxVisible: false,
      flowVisible: false,
      columnList: [{ prop: 'enCode', label: '订单编号' }, { prop: 'name', label: '客户名称' }, { prop: 'campusName', label: '校区名称' }, { prop: 'brand', label: '收款品牌' }, { prop: 'method', label: '收款方式' }, { prop: 'amount', label: '收款金额' }, { prop: 'time', label: '收款时间' }, { prop: 'type', label: '收款类型' }, { prop: 'status', label: '订单状态' }, { prop: 'managerId', label: '招商经理' }],
      brandOptions: [],
      methodOptions: [{ fullName: '刷卡', id: '1' }, { fullName: '转账', id: '2' }, { fullName: '扫码', id: '3' }, { fullName: '现金', id: '4' }],
      statusOptions: [{ fullName: '全款', id: '1' }, { fullName: '分期', id: '2' }],
      orderStatusOptions: [{ fullName: '完成付款', id: '1' }, { fullName: '部分付款', id: '2' }, { fullName: '部分退款', id: '3' }, { fullName: '已全额退款', id: '4' }],
      refundsFormVisible: false,
      printFormVisible: false
    }
  },
  computed: {},
  created() {
    this.initData()
  },
  mounted() {
    this.getUserList()
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  methods: {
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.managerIdOptions = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/crm/orders`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该条订单数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/crm/orders/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.search()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail)
      })
    },
    colseFlow(isrRefresh) {
      this.flowVisible = false
      if (isrRefresh) this.reset()
    },
    addPaymentsHandle(row, flowState) {
      let data = {
        id: '',
        enCode: '#visualDevorderpayment',
        flowId: '625865114699957509',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState,
        order: row,
        typeValue: 'addPayment'
      }

      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    updatePaymentsHandle(id, flowState) {
      let data = {
        id: id,
        enCode: '#visualDevorderpayment',
        flowId: '625865114699957509',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState,
        typeValue: 'updatePayment'
      }

      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    delPaymentHandle(id) {
      this.$confirm('此操作将永久删除该条缴费数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/crm/orders/payment/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.search()
              }
            })
          })
        })
        .catch(() => {})
    },
    expandVisible(row, rowIndex) {
      let data = row.row
      let res = []
      if (data.list && data.list.length > 0) {
        res.push('row-expand-has')
        return res
      } else {
        res.push('row-expand-cover')
        return res
      }
    },
    signHandle(row, flowState) {
      let data = {
        id: '',
        enCode: '#visualDevTransition',
        flowId: '334172229149394181',
        formType: 1,
        opType: flowState ? 0 : '-1',
        status: flowState,
        customerId: row.customerId,
        orderId: row.id
      }
      this.flowVisible = true
      this.$nextTick(() => {
        this.$refs.FlowBox.init(data)
      })
    },
    addRefundHandle(id, isDetail) {
      this.refundFormVisible = true
      this.$nextTick(() => {
        this.$refs.RefundForm.init(id, isDetail)
      })
    },

    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      if (this.formVisible) this.formVisible = false
      if (this.refundFormVisible) this.refundFormVisible = false
      if (this.recordsVisible) this.recordsVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    }
  }
}
